﻿@using Abp.Web.Mvc.Extensions;
@{
    ViewBag.ActiveMenu = "Student"; //The menu item will be active for this page.
}

<div>
    @*<h1><img src="~/images/abp-logo.png" style="padding-right: 10px;" />@string.Format(L("AboutFormat"), "ASP.NET Boilerplate")</h1>
        <p>@Html.Raw(L("About_AspNetBoilerplateDescription"))</p>
        <p>@L("ForMoreInformation"): <a href="http://www.aspnetboilerplate.com"><strong>aspnetboilerplate.com</strong></a>.</p>*@
    <div>学生列表</div>
    <div>
        <table style="width:500px;">
            <thead>
                <tr>
                    <td>编号</td>
                    <td>名称</td>
                    <td>班级</td>
                    <td>操作</td>
                </tr>
            </thead>
            <tbody class="student"></tbody>
        </table>

        <br />
        <div>
            <p>学生名称:<input type="text" id="studentName" /></p>
            <p>班级:
                <select id="classlist">
                </select>
            </p>
            <p><a href="javascript:void(0);" onclick="AddStudent()">添加</a></p>
        </div>
    </div>
</div>
<script src="~/Scripts/jquery-2.1.4.js"></script>
<script>
    $(function () {
        LoadClass();
        LoadStudent();
        //AddClass();
    });
    function LoadStudent() {
        abp.ajax({
            url: "/api/services/APP/Student/GetStudents",
            type: 'POST',
        }).done(function (data) {
            var str = "";
            for (var i = 0; i < data.length; i++) {
                str += "<tr>";
                str += "<td>" + data[i].id + "</td>";
                str += "<td>" + data[i].name + "</td>";
                str += "<td>" + data[i].className + "</td>";
                str += "<td><a href='javascript:UpdateStudent(" + data[i].id + ")'>编辑</a>--<a href='javascript:DeleteStudent(" + data[i].id + ")'>删除</a></td>";
                str += "</tr>";
            }
            $(".student").html(str);
        });

        
    }
    function LoadClass() {
        abp.ajax({
            url: "/api/services/APP/Class/GetClass",
            type: 'POST',
        }).done(function (data) {
            var str = "<option value=\"0\">请选择</option>";
            for (var i = 0; i < data.length; i++) {
                str += "<option value=\""+data[i].id+"\">"+data[i].name+"</option>";
            }
            $("#classlist").html(str);
        });

        
    }
    function AddStudent() {
        if ($.trim($("#studentName").val()) == "") {
            alert("学生名称不能为空");
            return;
        }
        //alert($("#classlist").val()); return;
        if ($("#classlist").val() == "0") {
            alert("请选择班级");
            return;
        }
       // alert($("#classlist").find("option:selected").text()); return;
        var newStudent = { name: $.trim($("#studentName").val()), cid: $("#classlist").val(), classname: $("#classlist").find("option:selected").text() };
        abp.ajax({
            url: "/api/services/APP/Student/addStudent",
            type: "post",
            data: JSON.stringify(newStudent)
        }).done(function (data) {
            //alert(data)
            if (data == -1) {
                //alert(data)
                alert("不能添加名称相同的学生");
            }
            else {
                LoadStudent();
            }
        });
    }
    function UpdateStudent(id) {
        $.ajax({
            url: "/api/services/APP/Student/updateStudent",
            type: "post",
            data: { "name": $.trim($("#className").val()) },
            success: function (data) {
                alert(JSON.stringify(data));
            },
            error: function (data) {
                //alert(JSON.stringify(data));
            }

        });
    }
    function DeleteStudent(id) {
        var newClass = { Id: id };
        abp.ajax({
            url: "/api/services/APP/Student/deleteStudent",
            type: "post",
            data: JSON.stringify(newClass)
        }).done(function (data) {
            LoadStudent();
            if (data == 1)
                alert("删除成功");
            else
                alert("删除失败");
        });
    }

</script>